//
//  BSForgetPasswordViewController.m
//  SuoSiApp
//
//  Created by zhang on 16/5/6.
//  Copyright © 2016年 Ym. All rights reserved.
//

#import "StringResponse.h"

#import "BSForgetPasswordViewController.h"

@interface BSForgetPasswordViewController ()
@property (nonatomic, assign) NSInteger num ;


@end

@implementation BSForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    _num = 60;
    
    [self addview];
    
    
    UIButton *button = BSButton;
    button.frame = CGRectMake(20, 25, 40, 30);
    //    button.backgroundColor = BSRed;
    [button setTitle:@"返回" forState:(UIControlStateNormal)];
    [button setTitleColor:BSBlack forState:(UIControlStateNormal)];
    
    [button addTarget:self action:@selector(aaaa) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:button];
}
//返回登陆界面
- (void)aaaa{
    
    
    
    [_changeTimer invalidate];
    _changeTimer = nil;
    
    [self dismissViewControllerAnimated:YES completion:^{
        nil;
    }];
}

- (void)addview{
    
    self.registView = [[BSRegistView alloc]initWithFrame:self.view.bounds];

    [self.view addSubview: _registView];

    self.registView.accountRegistLB.text = @"重置密码";
    
    
    
    [self.registView.getVerificatBT addTarget:self action:@selector(getVerification) forControlEvents:(UIControlEventTouchUpInside)];

    
    [self.registView.IDTF removeFromSuperview];
    [self.registView.IDNumberLB removeFromSuperview];
    [self.registView.agreenBT removeFromSuperview];
    [self.registView.protocolBT removeFromSuperview];
    
    [self.registView.registNowBT setTitle:@"保存" forState:(UIControlStateNormal)];
    
    [self.registView.registNowBT addTarget:self action:@selector(save) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.registView.registNowBT.enabled = YES;
}


// 获取验证码
- (void)getVerification{

    if (self.registView.accountTF.text.length == 11) {
        
        
        [PKRequestManager GetForgetVerifyCodeWithnetUrl:NetStr moblieNo:self.registView.accountTF.text method:POST finish:^(NSData *data) {
            StringResponse *strResponseModel = [[StringResponse alloc]init];
            [strResponseModel dealWithData:data];
        } error:^(NSError *error) {
            ;
        }];
        
        _changeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
        

    } else  {
        [PKRequestManager showAlertView:@"账号有误"];
    }

  }

// 定时器
- (void)timeChange{
    
    self.registView.getVerificatBT.enabled = NO;
    
    if (_num != 0) {
        _num = _num - 1;
        self.registView.timeLB.text = [NSString stringWithFormat:@"%ld%@",(long)_num,@"s"];
        
    }else{
        _num = 60;
        self.registView.getVerificatBT.enabled = YES;
        self.registView.timeLB.text = [NSString stringWithFormat:@"%ld%@",(long)_num,@"s"];
        
        [_changeTimer setFireDate:[NSDate distantFuture]];
    }
}

- (void)viewWillAppear:(BOOL)animated{
    

    
}
- (void)save{

    if ([self.registView.passwordTF.text isEqualToString:self.registView.confirmPasswordTF.text]) {
        
        [PKRequestManager ForgetPwdWithnetUrl:NetStr longName:self.registView.accountTF.text vaildCode:self.registView.verificationTF.text password:self.registView.confirmPasswordTF.text method:POST finish:^(NSData *data) {
            StringResponse *strResponseModel = [[StringResponse alloc]init];
            [strResponseModel dealWithData:data];
            
            if ([strResponseModel.Code isEqualToString:@"0"]) {
//                ViewController *vc = [[ViewController alloc]init];
                [self dismissViewControllerAnimated:YES completion:nil];
            }
            
        } error:^(NSError *error) {
            ;
        }];
        
        
    } else {
        [PKRequestManager showAlertView:@"输入密码不一致"];
    }
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
